package com.panels;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

import com.dataModel.Quran;
import com.utils.ResourcesLoader;

public class SouthPanel extends JPanel implements ActionListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 5041330898556558843L;

	private JButton nextSourahButton;

	private JButton previousSourahButton;

	private JLabel nextSourahLabel;

	private JLabel previousSourahLabel;

	public SouthPanel() {
		nextSourahButton = new JButton();
		previousSourahButton = new JButton();
		nextSourahLabel = new JLabel("");
		previousSourahLabel = new JLabel("");

		String fontName=previousSourahLabel.getFont().getName();
		int style=previousSourahLabel.getFont().getStyle();
		int size=20;
		Font labelFont=new Font(fontName,style,size);
		
		previousSourahLabel.setFont(labelFont);
		nextSourahLabel.setFont(labelFont);

		nextSourahButton.addActionListener(this);
		previousSourahButton.addActionListener(this);

		initGui();
	}

	private void initGui() {
		
		ImageIcon nextIcon =ResourcesLoader.getImageIcon("/com/data/images/next.gif");
		ImageIcon previousIcon =ResourcesLoader.getImageIcon("/com/data/images/previous.gif");
		nextSourahButton.setIcon(nextIcon);
		previousSourahButton.setIcon(previousIcon);
		nextSourahButton.setHorizontalTextPosition(SwingConstants.LEFT);
		this.add(nextSourahLabel);
		this.add(nextSourahButton);
		this.add(previousSourahButton);
		this.add(previousSourahLabel);
	}

	public void actionPerformed(ActionEvent e) {
		int currentPage = PagesPanel.getCurrentPage();
		int rightPageNumber = (currentPage % 2 == 0) ? currentPage - 1
				: currentPage;
		int leftPageNumber = (currentPage % 2 == 0) ? currentPage
				: currentPage + 1;
		if (e.getSource() == this.previousSourahButton) {
			int i = Quran.getQuranPages()[rightPageNumber].getStartAyah()
					.getSourahNumber();
			for (; i - 1 > 0; i--) {
				if (Quran.getSowar()[i - 1].getPageNumber() < rightPageNumber) {
					MainPanel.getMainPanel().getPagesPanel().displayPage(
							Quran.getSowar()[i - 1].getPageNumber());
					return;
				}
			}
		} else if (e.getSource() == this.nextSourahButton) {
			int i = Quran.getQuranPages()[leftPageNumber].getStartAyah()
					.getSourahNumber() + 1;

			for (; i - 1 < Quran.getSowar().length; i++) {
				if (Quran.getSowar()[i - 1].getPageNumber() > leftPageNumber) {
					MainPanel.getMainPanel().getPagesPanel().displayPage(
							Quran.getSowar()[i - 1].getPageNumber());
					return;
				}
			}
		}

	}

	public void setNextSourahName(String displayText) {
		nextSourahLabel.setText(displayText);
	}

	public void setPreviousSourahName(String displayText) {
		previousSourahLabel.setText(displayText);
	}

}
